<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
    >
      <el-form-item label="任务名称" prop="taskName">
        <el-input
            v-model="queryParams.taskName"
            placeholder="请输入任务名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
        />
      </el-form-item>

<!--      <el-form-item label="完成状态" prop="finishState">-->
<!--        <el-select-->
<!--            v-model="queryParams.finishState"-->
<!--            placeholder="请选择完成状态"-->
<!--            clearable-->
<!--            class="!w-240px"-->
<!--        >-->
<!--          <el-option-->
<!--              v-for="dict in getDictOptions(DICT_TYPE.BUSINESS_FINISH_STATUS)"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="发起部门" prop="creatorDept">
        <cascader-dept class="!w-240px" v-model="queryParams.creatorDept"/>
      </el-form-item>
      <el-form-item label="执行人" prop="executeUser">
        <cascader-user class="!w-240px" v-model="queryParams.executeUser"/>
      </el-form-item>
      <el-form-item label="截止时间" prop="deadline">
        <el-date-picker
            v-model="queryParams.deadline"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
            v-model="queryParams.createTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-input
            v-model="queryParams.priority"
            placeholder="请输入优先级"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
        />
      </el-form-item>
      <!--      <el-form-item label="标签" prop="label">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.label"-->
      <!--            placeholder="请输入标签"-->
      <!--            clearable-->
      <!--            @keyup.enter="handleQuery"-->
      <!--            class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="备注" prop="remark">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.remark"-->
      <!--            placeholder="请输入备注"-->
      <!--            clearable-->
      <!--            @keyup.enter="handleQuery"-->
      <!--            class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="协助人" prop="assistUser">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.assistUser"-->
      <!--            placeholder="请输入协助人"-->
      <!--            clearable-->
      <!--            @keyup.enter="handleQuery"-->
      <!--            class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px"/>
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px"/>
          重置
        </el-button>
        <el-button
            type="primary"
            plain
            @click="handleAdd()"

        >
          <Icon icon="ep:plus" class="mr-5px"/>
          新增
        </el-button>
        <!--        <el-button-->
        <!--            type="success"-->
        <!--            plain-->
        <!--            @click="handleExport"-->
        <!--            :loading="exportLoading"-->
        <!--        >-->
        <!--          <Icon icon="ep:download" class="mr-5px"/>-->
        <!--          导出-->
        <!--        </el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-tabs v-model="activeName" @tab-change="handleTabChange">
      <el-tab-pane label="我执行的" name="execute"/>
      <el-tab-pane label="我发起的" name="creator"/>
      <el-tab-pane v-if="checkPermi(['synergy:task-list:admin'])" label="全部数据" name="all"/>
    </el-tabs>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @row-dblclick="handleDetails"
    >

      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="任务名称" align="center" prop="taskName" min-width="200"/>
<!--      <el-table-column label="完成状态" align="center" prop="finishState">-->
<!--        <template #default="scope">-->
<!--          <dict-tag :type="DICT_TYPE.BUSINESS_FINISH_STATUS" :value="scope.row.finishState"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="发起人" align="center" prop="creator_dictText"/>
      <el-table-column label="发起部门" align="center" prop="creatorDept_dictText"/>
      <el-table-column label="执行人" align="center" prop="executeUser_dictText"/>
      <el-table-column
          label="截止时间"
          align="center"
          prop="deadline"
          :formatter="dateFormatter"
          width="180px"
      />
      <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="180px"
      />
      <el-table-column label="任务内容" align="center" prop="content"/>
      <el-table-column label="优先级" align="center" prop="priority"/>
      <!--      <el-table-column label="标签" align="center" prop="label" />-->
      <el-table-column label="关联文件" align="center" prop="taskFile"/>
      <!--      <el-table-column label="备注" align="center" prop="remark"/>-->
      <!--      <el-table-column label="协助人" align="center" prop="assistUser_dictText" />-->
      <el-table-column label="操作" align="center" min-width="120px" fixed="right">
        <template #default="scope">

          <el-button
              link
              type="primary"
              @click="handleDetails(scope.row)"
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />
  </ContentWrap>
  <WorkTaskForm ref="workTaskFormRef" @success="getList"/>
  <!-- 表单弹窗：添加/修改 -->
  <WorkTaskForm ref="formRef" @success="getList"/>
</template>

<script setup>
import {dateFormatter} from '@/utils/formatTime'
import download from '@/utils/download'
import {WorkTaskApi} from '@/api/business/synergy/work/task'
import WorkTaskForm from "@/views/business/synergy/work/components/workTaskForm.vue";
import {DICT_TYPE, getDictOptions} from "@/utils/dict";
import {useUserStore} from "@/store/modules/user";
import {checkPermi} from "@/utils/permission";
import router from "@/router";


/** 部门协同-工作任务 列表 */
defineOptions({name: 'SynergyWorkTaskList'})

const message = useMessage() // 消息弹窗
const {t} = useI18n() // 国际化

const userStore = useUserStore()
const activeName = ref("execute")
const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  taskName: undefined,
  parentId: undefined,
  finishState: undefined,
  creatorDept: undefined,
  executeUser: undefined,
  deadline: [],
  createTime: [],
  content: undefined,
  priority: undefined,
  label: undefined,
  taskFile: undefined,
  remark: undefined,
  assistUser: undefined
})
const queryFormRef = ref() // 搜索的表单
const workTaskFormRef = ref()
// const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  let userId = userStore.user.id
  if (activeName.value === "execute") {
    queryParams.creator = undefined
    queryParams.executeUser = userId
  } else if (activeName.value === "creator") {
    queryParams.executeUser = undefined
    queryParams.creator = userId
  } else {
    queryParams.executeUser = undefined
    queryParams.creator = undefined
  }

  loading.value = true
  try {
    const data = await WorkTaskApi.getWorkTaskPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}


/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await WorkTaskApi.exportWorkTask(queryParams)
    download.excel(data, '部门协同-工作任务.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const handleTabChange = (tab) => {
  activeName.value = tab
  handleQuery()
}

const handleDetails = (row) => {
  let id = row.id
  router.push({name: 'SynergyWorkTask', params: {id}})
}

const handleAdd = () => {
  workTaskFormRef.value.open()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
